<?php

declare(strict_types=1);
/**
 * This file is part of MoChat.
 * @link     https://mo.chat
 * @document https://mochat.wiki
 * @contact  group@mo.chat
 * @license  https://github.com/mochat-cloud/mochat/blob/master/LICENSE
 */
namespace MoChat\App\WorkContact\Action\Workbench\Tag;

use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\Middleware;
use Hyperf\HttpServer\Annotation\Middlewares;
use Hyperf\HttpServer\Annotation\RequestMapping;
use MoChat\App\Common\Middleware\WorkbenchAuthMiddleware;
use MoChat\App\WorkContact\Contract\WorkContactContract;
use MoChat\App\WorkContact\Contract\WorkContactEmployeeContract;
use MoChat\App\WorkEmployee\Contract\WorkEmployeeContract;
use MoChat\Framework\Action\AbstractAction;
use MoChat\Framework\Constants\ErrorCode;
use MoChat\Framework\Exception\CommonException;
use MoChat\Plugin\AutoTag\Action\Dashboard\Traits\AutoContactTag;

/**
 * 批量客户企业标签.
 *
 * Class BatchCreateContactTag
 * @Controller
 */
class BatchCreateContactTag extends AbstractAction
{
    use AutoContactTag;

    /**
     * @Inject
     * @var WorkEmployeeContract
     */
    protected $workEmployeeContract;

    /**
     * @Inject
     * @var WorkContactEmployeeContract
     */
    protected $workContactEmployeeContract;

    /**
     * @var
     */
    protected $contactTag;

    /**
     * @Inject
     * @var WorkContactContract
     */
    private $workContactContract;

    /**
     * @Middlewares({
     *     @Middleware(WorkbenchAuthMiddleware::class)
     * })
     * @RequestMapping(path="/workbench/workContactTag/batchCreateContactTag", methods="PUT")
     */
    public function handle(): array
    {
        $user = user();
        // 判断用户绑定企业信息
        if (! isset($user['corpId'])) {
            throw new CommonException(ErrorCode::INVALID_PARAMS, '未选择登录企业，不可操作');
        }
        $params['corpId'] = $user['corpId'];
        $params['userEmployeeId'] = $user['id'];
        $params['userId'] = $user['logUserId'];
        $params['tagIds'] = $this->request->input('tagIds');
        $params['contactIds'] = $this->request->input('contactIds');
        foreach ($params['contactIds'] as $item) {
            if ((int) $item === 0) {
                continue;
            }
            $data['tagArr'] = $params['tagIds'];
            $data['contactId'] = $item;
            // 员工id
            $contactEmployee = $this->workContactEmployeeContract->getWorkContactEmployeeByCorpIdContactId($item, user()['corpIds'][0], ['employee_id']);
            if (empty($contactEmployee)) {
                continue;
            }
            $data['employeeId'] = $contactEmployee['employeeId'];
            // 客户
            $contact = $this->workContactContract->getWorkContactById($item, ['wx_external_userid']);
            $data['contactWxExternalUserid'] = $contact['wxExternalUserid'];
            // 员工
            $employee = $this->workEmployeeContract->getWorkEmployeeById($contactEmployee['employeeId'], ['wx_user_id']);
            $data['employeeWxUserId'] = $employee['wxUserId'];
            $this->autoTag($data);
        }

        return [];
    }

    /**
     * @return string[] 规则
     */
    public function rules(): array
    {
        return [
            'tagIds' => 'required|array',
            'contactIds' => 'required|array',
        ];
    }

    /**
     * 获取已定义验证规则的错误消息.
     */
    public function messages(): array
    {
        return [
            'tagIds.required' => '标签id 必传',
            'contactIds.required' => '客户id 必传',
        ];
    }
}
